package produce

import (
	"data-process/config"
	"data-process/logger"
	"data-process/storage"
	"encoding/json"
	"sync"
)

const (
	DATABASE  = "database"
	DATAMODLE = "mock"
)

type CommonProducer interface {
	producer()
}

type Produce struct {
	DataPool  chan map[string]interface{} // 存储构建的数据的池子, 用于存放构建好的模型数据
	wg        sync.WaitGroup
	Type      string
	Size      int
	ModelKeys []string // 顺序的keys
	producer  func()
}

func DefaultProduce() {
	types := config.SysConfig.Produce.Type
	if types == DATABASE {
		logger.Infof("数据将同步到文件中")
		DefaultDbProduce().Run()
	} else if types == DATAMODLE {
		logger.Infof("数据将同步到数据表中")
		DefaultMockProduce().Run()
	}
}

func (p *Produce) Run() {
	logger.Info("数据处理开始启动")
	go p.producer()
	//go p.SimpleConsumer()
	go p.consumer()
	p.wg.Add(2)
	p.wg.Wait()
}

func (p *Produce) SimpleConsumer() {
	for {
		data, ok := <-p.DataPool
		if !ok {
			break
		}
		content, _ := json.Marshal(data)
		logger.Infof("获取的数据是：%s\n", string(content))
	}
}

// consumer 消费数据
func (p *Produce) consumer() {
	storage.NewStorage(config.SysConfig.Storage, p.ModelKeys).Do(p.DataPool)
	defer func() {
		p.wg.Done()
	}()
}
